﻿package 
{
	
import com.electrotank.electroserver5.ElectroServer;
import com.electrotank.electroserver5.api.ConnectionResponse;
import com.electrotank.electroserver5.api.JoinRoomEvent;
import com.electrotank.electroserver5.api.JoinRoomRequest;
import com.electrotank.electroserver5.api.LoginRequest;
import com.electrotank.electroserver5.api.LoginResponse;
import com.electrotank.electroserver5.api.MessageType;
import com.electrotank.electroserver5.connection.AvailableConnection;
import com.electrotank.electroserver5.connection.TransportType;
import com.electrotank.electroserver5.server.Server;
import com.electrotank.electroserver5.util.CryptoTools;
import com.electrotank.electroserver5.util.ES5TraceAdapter;
import com.electrotank.logging.adapter.ILogger;
import com.electrotank.logging.adapter.Log;

import fl.controls.Button;
import fl.controls.List;
import fl.controls.TextArea;
import fl.controls.TextInput;
import fl.data.DataProvider;

import flash.display.Loader;
import flash.display.MovieClip;
import flash.events.*;
import flash.events.Event;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;



public class login extends MovieClip{
	private var _userName:TextInput;
	private var _passWord:TextInput;
	private var _submit:Button;
	private var _es:ElectroServer;
	private var _hashId:int = 0;
	
	private var lobbyMan:LobbyManager;
	private var _game:MovieClip;
	
	private var _currentGame:String;
	
	
	public function login(){
		trace("90909090909090");
		
		Log.setLogAdapter(new ES5TraceAdapter());
		_es = new ElectroServer();
		var server:Server = new Server("server1"); 
		var availCon:AvailableConnection = new AvailableConnection("127.0.0.1", 9899, TransportType.BinaryTCP); 
		server.addAvailableConnection(availCon); 
		_es.engine.addServer(server); 
		
		_es.engine.connect(); 


		this.removeChild(loginPanel);
		_userName = this.loginPanel.userName;
		_passWord = this.loginPanel.passWord;
		_submit = this.loginPanel.submit;
		_submit.addEventListener(MouseEvent.CLICK,onSubmit);
		_es.engine.addEventListener(MessageType.ConnectionResponse.name, onConnectionResponse);
		_es.engine.addEventListener(MessageType.LoginResponse.name, onLoginResponse);
		//_es.engine.addEventListener(MessageType.JoinRoomEvent.name, onJoinRoomEvent);
		

		
		
	}
	
	private function onConnectionResponse(e:ConnectionResponse):void {
		if (e.successful) {
			//add the login panel so the user can login
			removeChild(waittingPanel);
			addChild(loginPanel);
			_hashId = e.hashId;
			trace("Connection hashId: " + _hashId);
		} else {
			trace( "Connection failed!");
		}
	}
	
	private function onSubmit(e:MouseEvent):void{
		
		var lr:LoginRequest = new LoginRequest();
		
		lr.userName = _userName.text;
		lr.password = new CryptoTools().generatePasswordHash(_passWord.text, _hashId);
		
		trace("LoginRequest: " + _userName.text + ", " + _hashId + ", " + lr.password);
		
		_es.engine.send(lr);
		
		this.removeChild(loginPanel);
		this.addChild(waittingPanel);
	}
	
	private function onLoginResponse(e:LoginResponse):void {
		if (e.successful) {
		
			createLobby();
		} else {
			trace( "Login failed!");
		}
	}
	
	private function completeHandler(event:Event):void{
		trace("load complete");
		var Lobby:Class = Class( ApplicationDomain.currentDomain.getDefinition("Lobby") );
		var myLobby = new Lobby();
		trace(myLobby);
		
		this.removeChild(waittingPanel);
		this.addChild(myLobby);
		
		trace("add lobbymanager");
		lobbyMan = new LobbyManager();
		
		lobbyMan.addEventListener(LobbyEvent.STARTGAME, onGameStart);
		trace("add lobbymanager");
		lobbyMan.lobby = myLobby;
		trace("add lobbymanager 2");
		lobbyMan.es = _es;
		trace("add lobbymanager 3");
		lobbyMan.init();
		
		

	}
	
	private function createLobby(){
		trace("Login success");
		
		var ldr = new Loader();
		var req:URLRequest = new URLRequest("lobby.swf");
		var ldrContext:LoaderContext = new LoaderContext(false, 
			ApplicationDomain.currentDomain);
		ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, 
			completeHandler);
		ldr.load(req, ldrContext);
	}
	
	private function onGameStart(event:LobbyEvent){
		
		this._currentGame = String(event.param);
		trace("game start:"+this._currentGame);
		var ldr = new Loader();
		var req:URLRequest = new URLRequest("game/"+String(event.param)+"/"+String(event.param)+".swf");
		var ldrContext:LoaderContext = new LoaderContext(false, 
			ApplicationDomain.currentDomain);
		ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, 
			gameLoadOver);
		ldr.load(req, ldrContext);
		
		
	}
	
	private function gameLoadOver(event:Event):void{
		switch(this._currentGame){
			case "DiggingGame":
				var DigGame:Class = Class( ApplicationDomain.currentDomain.getDefinition("com.gamebook.dig.DigGame") );
				_game = new DigGame();
				break;
			case "CoopGame":
				var CoopGame:Class = Class( ApplicationDomain.currentDomain.getDefinition("com.gamebook.coop.CoopGame") );
				_game = new CoopGame();
				break;
		}
		//新游戏在这里添加
		
		
		//_game = new DigGame();
		_game.es = _es;
		_game.room = lobbyMan._gameRoom;
		
		//listen for when the game is done
		_game.addEventListener(LobbyEvent.BACKTOLOBBY, onDigGameBackToLobby);
		
		//initialize the game and add it to the screen
		_game.initialize();
		this.addChild(_game);
		
		//tell the lobby it is about to be removed (it will clean up), then remove it
		removeChild(lobbyMan._lobby);
		lobbyMan.destroy();
		lobbyMan.lobby = null;
		lobbyMan = null;
		trace("remove lobby");
	}
	
	private function onDigGameBackToLobby(e:Event):void {
		//var DigGame:Class = Class( ApplicationDomain.currentDomain.getDefinition("com.gamebook.dig.DigGame") );
		//destroy and remove the game
		_game.destroy();
		removeChild(_game);
		_game.removeEventListener(LobbyEvent.BACKTOLOBBY, onDigGameBackToLobby);
		_game = null;
		
		//create the lobby
		this.addChild(waittingPanel);
		createLobby();
	}
	
	

	
}
}







